package com.zcsy.manage.service.mall.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.manage.model.mall.GoodsSpecification;
import com.zcsy.manage.model.mall.GoodsSpecificationAttr;
import com.zcsy.manage.persistence.mall.GoodsSpecificationDao;
import com.zcsy.manage.persistence.weixin.StringUtil;
import com.zcsy.manage.service.mall.IGoodsSpecificationService;

/**
 * @Title: GoodsSpecificationServiceImpl.java
 * @Package： com.zcsy.manage.service.mall.impl 
 * @Description: 商品规格service层实现类
 *
 * @author caiyinli 13277030520@163.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年5月12日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */

@Service
public class GoodsSpecificationServiceImpl implements IGoodsSpecificationService {
	
	@Autowired 
	private GoodsSpecificationDao goodsSpecificationDao;

	@Override
	public List<GoodsSpecification> getGoodsSpecificationByGoodsId(
			String goodsId) {
		return goodsSpecificationDao.getGoodsSpecificationByGoodsId(goodsId);
	}

	@Override
	public GoodsSpecification getGoodsSpecificationById(String id) {
		return goodsSpecificationDao.getGoodsSpecificationById(id);
	}

	@Override
	public int insert(GoodsSpecification specification) {
		return goodsSpecificationDao.insert(specification);
	}

	@Override
	public int update(GoodsSpecification specification) {
		return goodsSpecificationDao.update(specification);
	}

	@Override
	public int updateGoodsSpecificationById(GoodsSpecification record) {
		return goodsSpecificationDao.updateGoodsSpecificationById(record);
	}

    @Override
    public List<GoodsSpecification> getGoodsSpecificationDetailByGoodsId(String goodsId) {
        return goodsSpecificationDao.getGoodsSpecificationDetailByGoodsId(goodsId);
    }

    @Override
    public GoodsSpecification getDefaultGoodsSpec(String goodsId) {
        return goodsSpecificationDao.getDefaultGoodsSpec(goodsId);
    }

    @Override
    public GoodsSpecification getGoodsSpedByAttr(String goodsId, String attrValues) {
        GoodsSpecification specInfo = null;
        List<GoodsSpecification> specList = goodsSpecificationDao.getGoodsSpecificationDetailByGoodsId(goodsId);
        StringBuilder attrStr = null;
        for(GoodsSpecification spec : specList) {
            attrStr = new StringBuilder();
            for(GoodsSpecificationAttr attr : spec.getGoodsSpecificationAttrs()) {
                if(StringUtil.isEmptyString(attrStr.toString())) {
                    attrStr.append(attr.getAttrValue());
                } else {
                    attrStr.append(",").append(attr.getAttrValue());
                }
                
            }
            if(StringUtil.compareArray(attrValues.split(","), attrStr.toString().split(","))) {
                specInfo = spec;
                break;
            }
        }
        // 根据选择中的一个属性值查询出可选的属性并默认选择中一个
        String attrValue = attrValues.split(",")[attrValues.split(",").length - 1];
        if(null == specInfo) {
            List<GoodsSpecification> list = goodsSpecificationDao.getDefaultGoodsSpecBySpecId(goodsId, attrValue);
            if(null != list && list.size() > 0) {
                specInfo = goodsSpecificationDao.getGoodsSpecById(list.get(0).getId());
            }
        }
        return specInfo;
    }

    @Override
    public GoodsSpecification getGoodsSpecById(String specId) {
        return goodsSpecificationDao.getGoodsSpecById(specId);
    }

}
